import { BaseModel } from './BaseModel.js';
import { dataFiles } from '../config/database.js';

const defaultClassGrades = [
  {
    id: 1,
    className: '计算机科学与技术1班',
    courseName: '企业级人工智能项目实训一',
    courseCode: 'AI001',
    teacher: '曲凤才',
    semester: '2023-2024-2',
    academicYear: '2023-2024',
    totalStudents: 45,
    averageScore: 78.5,
    maxScore: 95,
    minScore: 62,
    passRate: 88.9,
    excellentRate: 22.2,
    gradeDistribution: {
      '90-100': 10,
      '80-89': 20,
      '70-79': 10,
      '60-69': 5,
      '0-59': 0
    },
    status: 'published',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 2,
    className: '软件工程2班',
    courseName: '企业级人工智能项目实训二',
    courseCode: 'AI002',
    teacher: '曲凤才',
    semester: '2023-2024-2',
    academicYear: '2023-2024',
    totalStudents: 38,
    averageScore: 82.3,
    maxScore: 96,
    minScore: 68,
    passRate: 94.7,
    excellentRate: 28.9,
    gradeDistribution: {
      '90-100': 11,
      '80-89': 18,
      '70-79': 7,
      '60-69': 2,
      '0-59': 0
    },
    status: 'published',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  }
];

export class ClassGrade extends BaseModel {
  constructor() {
    // 使用配置的正确文件路径
    super(dataFiles.classGrades, defaultClassGrades);
  }

  findByClassName(className) {
    const data = this.findAll();
    return data.filter(classGrade => classGrade.className === className);
  }

  findByCourseCode(courseCode) {
    const data = this.findAll();
    return data.filter(classGrade => classGrade.courseCode === courseCode);
  }

  findBySemester(semester) {
    const data = this.findAll();
    return data.filter(classGrade => classGrade.semester === semester);
  }

  // 添加安全检查
  findAll() {
    const data = super.findAll();
    return Array.isArray(data) ? data : [];
  }
}